package com.shengyijie.activity.commen;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.HttpResponse;

import com.shengyijie.activity.MainActivity;
import com.shengyijie.activity.R;     
import com.shengyijie.activity.UserTabActivity;
import com.shengyijie.activity.share.ShareSelectActivity;
import com.shengyijie.activity.tab.RecommendedActivity;
import com.shengyijie.adapter.view.AlertBox;
import com.shengyijie.adapter.view.GalleryFlow;
import com.shengyijie.adapter.view.AlertBox.OnCanceledListener;
import com.shengyijie.adapter.viewadapter.GalleryAdapter;
import com.shengyijie.context.ContextApplication;
import com.shengyijie.model.http.HttpConnectApi;   
import com.shengyijie.model.json.JsonParser;
import com.shengyijie.model.object.Listobject.AdvertPicList;
import com.shengyijie.model.object.Listobject.ProPictureList;
import com.shengyijie.model.object.baseobject.GuesstBook;
import com.shengyijie.util.DataStore;
import com.shengyijie.util.Utility;

import android.app.Activity; 
import android.app.AlertDialog;
import android.app.ProgressDialog; 
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle; 
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.view.View.OnClickListener;  
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton; 
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TabHost;
import android.widget.TableLayout;
import android.widget.TextView; 
import android.widget.Toast;

public class ItemActivity extends Activity implements OnClickListener{
     
	public Task task;  
	private HttpConnectApi httpConnect;
	
	private GalleryFlow galleryFlow;
	private ScrollView scrollView;
	
	 private ImageButton tabHome;
	 private ImageButton tabCategory;
	 private ImageButton tabRecommended;
	 private ImageButton tabUser;
	 private ImageButton tabMore;
	 
	 private TableLayout tab1;
	 private TableLayout tab2;
	 
	 private ImageView im_logo;
	 private TextView tv_name;
	 private TextView tv_define;
	 private TextView tv_des;
	 private Button attention;
	 private Button bt_message;
	 private TextView tv_fenxi;
	 
	 private EditText et_name; 
	 private EditText et_tel;
	 private EditText et_email;
	 private EditText et_address;
	 private EditText et_message;
	 private Button commit;
	 
	 private String name; 
	 private String tel;
	 private String email;
	 private String address;
	 private String message;
	 
	
    public void onCreate(Bundle savedInstanceState){
         super.onCreate(savedInstanceState);
         requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
         setContentView(R.layout.item);
         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,      // 注意顺序
                 R.layout.title);
         initView(); 
         setItemInfo();
         preTask=5;
		    task=new Task();
		    task.execute(TASK_LOAD_PIC);
         
    }
     
    public void initView(){
    	TextView title=(TextView) this.findViewById(R.id.title);
		title.setText(ContextApplication.project.getPro_name()); 
		
		Button back=(Button) this.findViewById(R.id.back);
		back.setVisibility(View.VISIBLE);
		back.setOnClickListener(this);
		
		Button share=(Button) this.findViewById(R.id.right);
		share.setVisibility(View.VISIBLE);
		share.setOnClickListener(this);
		
		galleryFlow = (GalleryFlow) findViewById(R.id.gallery); 
		scrollView = (ScrollView) findViewById(R.id.sc); 
		
		im_logo=(ImageView) this.findViewById(R.id.image);
		tv_name=(TextView) this.findViewById(R.id.name);
		tv_define=(TextView) this.findViewById(R.id.define);
		tv_des=(TextView) this.findViewById(R.id.des);
		attention=(Button) this.findViewById(R.id.attention);
		attention.setOnClickListener(this);
		bt_message=(Button) this.findViewById(R.id.message);
		bt_message.setOnClickListener(this);
		tv_fenxi=(TextView) this.findViewById(R.id.fenxi);
		 
		et_name=(EditText) this.findViewById(R.id.mname); 
	    et_tel=(EditText) this.findViewById(R.id.tel);
		et_email=(EditText) this.findViewById(R.id.email);
		et_address=(EditText) this.findViewById(R.id.address);
		et_message=(EditText) this.findViewById(R.id.mmessage);
		commit=(Button) this.findViewById(R.id.commit);
		commit.setOnClickListener(this);
		
		 tab1=(TableLayout) this.findViewById(R.id.tab1);
		 tab2=(TableLayout) this.findViewById(R.id.tab2);
		 if(ContextApplication.tabtype==0){
			 tab1.setVisibility(View.VISIBLE);
			 tabHome=(ImageButton) this.findViewById(R.id.home);
	    	 tabCategory=(ImageButton) this.findViewById(R.id.category);
	    	 tabRecommended=(ImageButton) this.findViewById(R.id.recommended);
	    	 tabUser=(ImageButton) this.findViewById(R.id.user);
	    	 tabMore=(ImageButton) this.findViewById(R.id.more);
		 }else{
			 tab2.setVisibility(View.VISIBLE);
			 tabHome=(ImageButton) this.findViewById(R.id.home1);
		     tabCategory=(ImageButton) this.findViewById(R.id.category1);
		     tabRecommended=(ImageButton) this.findViewById(R.id.recommended1);
		     tabUser=(ImageButton) this.findViewById(R.id.user1);
		     tabMore=(ImageButton) this.findViewById(R.id.more1);
		 }
		 
    	 
		try{
		    switch(ContextApplication.prePage){
		       case ContextApplication.PAGE_HOME:
		    	   tabHome.setSelected(true);
			       break;
		       case ContextApplication.PAGE_CATEGORY:
		    	   tabCategory.setSelected(true);
			       break;
		       case ContextApplication.PAGE_RECOMMENDED:
		    	   tabRecommended.setSelected(true);
			       break;
		       case ContextApplication.PAGE_USER:
		    	   tabUser.setSelected(true);
			       break;
		       case ContextApplication.PAGE_MORE:
		    	   tabMore.setSelected(true);
			       break;
		     }
		 }catch(Exception e){}
		
		 tabHome.setOnClickListener(this);
		 tabCategory.setOnClickListener(this);
		 tabRecommended.setOnClickListener(this);
		 tabUser.setOnClickListener(this);
		 tabMore.setOnClickListener(this);
		 
		 mypDialog=new AlertBox(this,this,findViewById(R.id.item_page)); 
		 mypDialog.setCompletedListener(new OnCanceledListener(){ 
				@Override
				public void onCanceled() { 
					if(task.getStatus()==AsyncTask.Status.RUNNING){
			    		 task.cancel(true); 
					  }
				}});
    }
    
    public void setItemInfo(){
    	try{
    		if(ContextApplication.project!=null){
    			tv_name.setText(ContextApplication.project.getPro_name());
    			tv_define.setText("招商电话-"+ContextApplication.project.getZs_phone());
    			tv_des.setText(ContextApplication.project.getPro_about()); 
    			tv_fenxi.setText(ContextApplication.project.getPro_fenxi());
    			 
    			et_name=(EditText) this.findViewById(R.id.mname); 
    			et_name.addTextChangedListener(new TextWatch()); 
    		    et_tel=(EditText) this.findViewById(R.id.tel);
    		    et_tel.addTextChangedListener(new TextWatch()); 
    			et_email=(EditText) this.findViewById(R.id.email);
    			et_email.addTextChangedListener(new TextWatch()); 
    			et_address=(EditText) this.findViewById(R.id.address);
    			et_address.addTextChangedListener(new TextWatch()); 
    			et_message=(EditText) this.findViewById(R.id.mmessage);
    			et_message.addTextChangedListener(new TextWatch()); 
    			commit=(Button) this.findViewById(R.id.commit);
    			
    			GuesstBook guesstBook=DataStore.fetchMessage(this);
    			et_name.setText(guesstBook.getName());
    			et_tel.setText(guesstBook.getMobile());
    			et_email.setText(guesstBook.getEmail());
    			et_address.setText(guesstBook.getAddress());
    			et_message.setText(guesstBook.getContent());
    			
    			new Task().execute(TASK_IMAGE);
        	}
    	}catch(Exception e){}
    	 
    }
    
    class TextWatch implements TextWatcher {  
        public void beforeTextChanged(CharSequence s, int start, int count,  
                int after) {   
        }    
        public void onTextChanged(CharSequence s, int start, int before,  
                int count) {  
        	if(et_name.hasFocus()){
        		DataStore.storeMessage(ItemActivity.this, "name", s.toString());
        	}else if(et_tel.hasFocus()){
        		DataStore.storeMessage(ItemActivity.this, "mobile", s.toString());
        	}
        	else if(et_email.hasFocus()){
        		DataStore.storeMessage(ItemActivity.this, "email", s.toString());
        	}
        	else if(et_address.hasFocus()){
        		DataStore.storeMessage(ItemActivity.this, "address", s.toString());
        	}
        	else if(et_message.hasFocus()){
        		DataStore.storeMessage(ItemActivity.this, "content", s.toString());
        	}
        	
  
        }  
  
        @Override  
        public void afterTextChanged(Editable s) {  
  
        }  
  
    }   
    
    public ArrayList<String> contentList=new ArrayList<String>();
    GalleryAdapter adapter;
    public void setImageAdapter(){
		 try{  
			 if( ContextApplication.project.getAdverPicList()!=null&& ContextApplication.project.getAdverPicList().getCount()>0)
			 {
				 for (int i = 0; i < ContextApplication.project.getAdverPicList().getCount(); i++) {  
						contentList.add(ContextApplication.project.getAdverPicList().getItem(i).getPic_url());
					 } 
					 adapter = new GalleryAdapter(this,this, contentList,galleryFlow);
					 galleryFlow.setAdapter(adapter);
			 }else{
				 
				 galleryFlow.setAdapter(null);
			 }
				
		 }catch(Exception e){}
	 }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {		
		   if(requestCode==1){
				if (resultCode != RESULT_OK) {
					return;
				}
			   try{
			       String state = data.getStringExtra("state");
			       if(state.equals("exit")){
			    	   Bundle bd = new Bundle();
			           bd.putString("state", "exit");	
			           Intent it = new Intent();
			           it.putExtras(bd);
			           setResult(RESULT_OK, it); 
			    	   this.finish();
			       }
			   }
			   catch(Exception e){}
		   }
	 }

	@Override
	public void onClick(View v) { 
		Bundle bd = new Bundle();
        bd.putString("state", "exit");	
        Intent it = new Intent();
        it.putExtras(bd);
        TabHost tabhost;
        if(ContextApplication.tabtype==0){
        	tabhost=MainActivity.tabhost;
        }else{
        	tabhost=UserTabActivity.tabhost;
        } 
		switch(v.getId()){
		     case R.id.home:
			    try{
				   if(ContextApplication.prePage!=ContextApplication.PAGE_HOME){
					   tabhost.setCurrentTab(0);
					   setResult(RESULT_OK, it); 
					   this.finish();
				   }
			    }catch(Exception e){}
			    break;
		     case R.id.category:
				    try{
					   if(ContextApplication.prePage!=ContextApplication.PAGE_CATEGORY){
						   tabhost.setCurrentTab(1);
						   setResult(RESULT_OK, it); 
						   this.finish();
					   }
				    }catch(Exception e){}
				    break;
		     case R.id.recommended:
				    try{
					   if(ContextApplication.prePage!=ContextApplication.PAGE_RECOMMENDED){
						   tabhost.setCurrentTab(2);
						   setResult(RESULT_OK, it); 
						   this.finish();
					   }
				    }catch(Exception e){}
				    break;
		     case R.id.user:
				    try{
					   if(ContextApplication.prePage!=ContextApplication.PAGE_USER){
						   tabhost.setCurrentTab(3);
						   setResult(RESULT_OK, it); 
						   this.finish();
					   }
				    }catch(Exception e){}
				    break;
		     case R.id.more:
				    try{
					   if(ContextApplication.prePage!=ContextApplication.PAGE_MORE){
						   tabhost.setCurrentTab(4);
						   setResult(RESULT_OK, it); 
						   this.finish();
					   }
				    }catch(Exception e){}
				    break;
				    case R.id.home1:
					    try{
						   if(ContextApplication.prePage!=ContextApplication.PAGE_HOME){
							   tabhost.setCurrentTab(0);
							   setResult(RESULT_OK, it); 
							   this.finish();
						   }
					    }catch(Exception e){}
					    break;
				     case R.id.category1:
						    try{
							   if(ContextApplication.prePage!=ContextApplication.PAGE_CATEGORY){
								   tabhost.setCurrentTab(1);
								   setResult(RESULT_OK, it); 
								   this.finish();
							   }
						    }catch(Exception e){}
						    break;
				     case R.id.recommended1:
						    try{
							   if(ContextApplication.prePage!=ContextApplication.PAGE_RECOMMENDED){
								   tabhost.setCurrentTab(2);
								   setResult(RESULT_OK, it); 
								   this.finish();
							   }
						    }catch(Exception e){}
						    break;
				     case R.id.user1:
						    try{
							   if(ContextApplication.prePage!=ContextApplication.PAGE_USER){
								   tabhost.setCurrentTab(3);
								   setResult(RESULT_OK, it); 
								   this.finish();
							   }
						    }catch(Exception e){}
						    break;
				     case R.id.more1:
						    try{
							   if(ContextApplication.prePage!=ContextApplication.PAGE_MORE){
								   tabhost.setCurrentTab(4);
								   setResult(RESULT_OK, it); 
								   this.finish();
							   }
						    }catch(Exception e){}
						    break;
		     case R.id.right:
				    try{
				    	ContextApplication.shareName=ContextApplication.project.getPro_name();
				    	ContextApplication.shareUrl=ContextApplication.project.getPro_url();
				    	if(ContextApplication.shareUrl==null||ContextApplication.shareUrl.equals("")){
				    		ContextApplication.shareUrl="http://www.shengyijie.net";
				    	}
				    	ContextApplication.shareContent="我在生意街商机网发现了“"+ContextApplication.shareName+
						 "”,觉得很不错，分享给大家...\n"+ContextApplication.shareUrl;
				    	Intent i=new Intent();
						i.setClass(this, ShareSelectActivity.class);
						this.startActivity(i);
				    }catch(Exception e){}
				    break;
		     case R.id.back:
				    try{
					  this.finish();
				    }catch(Exception e){}
				    break;
		     case R.id.attention:
		    	   
				    if(ContextApplication.isUserLogin){
				    	    preTask=0;
						    task=new Task();
						    task.execute(TASK_ATTENTION);
				    }else{
				    	 Toast.makeText(ItemActivity.this, "恭喜您，关注成功", Toast.LENGTH_SHORT).show();	
				    }
				    break;
		     case R.id.message:
		    	    TextView msg=(TextView) this.findViewById(R.id.l_msg);
		    	    scrollView.scrollTo(0, msg.getTop());
				    break;
		     case R.id.commit:
		    	    name=et_name.getText().toString().trim();
		    	    tel=et_tel.getText().toString().trim();
		    	    email=et_email.getText().toString().trim();
		    	    address=et_address.getText().toString().trim();
		    	    message=et_message.getText().toString().trim(); 
		    	   if(name.equals("")){
		    		   Toast.makeText(ItemActivity.this, "请填写您的姓名", Toast.LENGTH_SHORT).show();	
		    		   et_name.requestFocus();
		    	   }else if(tel.equals("")){
		    		   Toast.makeText(ItemActivity.this, "请填写您的联系电话", Toast.LENGTH_SHORT).show();	
		    		   et_tel.requestFocus();
		    	   }else if(!Utility.isMobile(tel)){
		    		   Toast.makeText(ItemActivity.this, "请填写正确的手机号", Toast.LENGTH_SHORT).show();	
		    		   et_tel.requestFocus();
		    	   }else if(!email.equals("")&&!Utility.isEmail(email)){
		    		   Toast.makeText(ItemActivity.this, "请填写正确的邮箱号", Toast.LENGTH_SHORT).show();	
		    		   et_email.requestFocus();
		    	   }else{
		    		    preTask=1;
					    task=new Task();
					    task.execute(TASK_MESSAGE);
		    	   } 
				   break;
		}
	}
	
   
	public String doAttentionPro(){
    	String result="ap_fail";
        httpConnect=HttpConnectApi.getInstance();
        try {
            HttpResponse response=httpConnect.attentionPro(ContextApplication.project.getID(), ContextApplication.user); 
            if (response.getStatusLine().getStatusCode() == 200)
			{ 
           	    JsonParser jsonParser=JsonParser.getInstance();
           	    if(jsonParser.getResultCode(response)==ContextApplication.RESULT_CODE_OK) 
           	    	result="ap_ok"; 
			}
            return result;
        }catch (Exception e) {} 
		return result;
   } 
	
	public String doGuestBook(){
    	String result="gb_fail";
        httpConnect=HttpConnectApi.getInstance();
        try {
            HttpResponse response=httpConnect.guestBook(ContextApplication.project.getPro_id(), name,
            		tel, email, address, message, ContextApplication.user);
            if (response.getStatusLine().getStatusCode() == 200)
			{ 
           	    JsonParser jsonParser=JsonParser.getInstance();
           	    if(jsonParser.getResultCode(response)==ContextApplication.RESULT_CODE_OK) 
           	    	result="gb_ok"; 
			}
            return result;
        }catch (Exception e) {} 
		return result;
 }
	
	ProPictureList proPictureList;
    public String doGetProPicList(){
     	String result="pp_fail";
        httpConnect=HttpConnectApi.getInstance();
        try { 
            HttpResponse response=httpConnect.getProjectPic(ContextApplication.project.getID());
            if (response.getStatusLine().getStatusCode() == 200)
			{ 
           	    JsonParser jsonParser=JsonParser.getInstance();
           	    AdvertPicList advertPicList =jsonParser.getAderts(response);  
           	    if(advertPicList!=null){
           	    	ContextApplication.project.setAdverPicList(advertPicList);
           	    	if(advertPicList.getCount()>0)
           	    	    result="pp_ok";
           	    	else if(advertPicList.getCount()==0)
           	    		result="pp_0";
           	    } 
			}
            return result;
        }catch (Exception e) {} 
		return result;
   }
	
	
	 int preTask=0;
	 private AlertBox mypDialog;
      
	 private final static int ALERT_LOADING=2;
	 private final static int ALERT_MESSAGE=1;
	  
	 
	 private final static int RESULT_LOAD_ITEMDETAIL_OK=3;
	 private final static int RESULT_LOAD_ITEMDETAIL_FAIL=4;
	 
	 private final static int RESULT_ATTENTION_OK=5;
	 private final static int RESULT_ATTENTION_FAIL=6;
	 
	 private final static int RESULT_MESSAGE_OK=7;
	 private final static int RESULT_MESSAGE_FAIL=8;
	 
//	 private final static int RESULT_LOAD_ITEMDETAIL_OK=9;
//	 private final static int RESULT_LOAD_ITEMDETAIL_FAIL=10;
	 
	 
	 
	  
	   
	 private final static String TASK_ATTENTION="attention";   
	 private final static String TASK_MESSAGE="message"; 
	 private final static String TASK_IMAGE="image"; 
	 private final static String TASK_LOAD_PIC="loadpic";   
	 private final Handler handler = new Handler(Looper.getMainLooper()) {
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
			 	switch (msg.what) { 
				   case ALERT_LOADING: 
					    mypDialog.setNoticeMessage("正在发送关注请求，请稍后...");
					    mypDialog.show();
					    break; 
				   case ALERT_MESSAGE: 
					    mypDialog.setNoticeMessage("正在留言，请稍后...");
					    mypDialog.show();
					    break; 
                    case RESULT_LOAD_ITEMDETAIL_OK:
                    	 setImageAdapter(); 
					     break; 
                    case RESULT_LOAD_ITEMDETAIL_FAIL:
					     mypDialog.dismiss(); 
					     Toast.makeText(ItemActivity.this, "对不起，暂无数据", Toast.LENGTH_SHORT).show();	  
					     break;
                    case RESULT_ATTENTION_OK:
                  	     mypDialog.dismiss();
                  	     ContextApplication.isAttentionRefresh=true;
                  	     Toast.makeText(ItemActivity.this, "恭喜您，关注成功", Toast.LENGTH_SHORT).show();
					     break; 
                   case RESULT_ATTENTION_FAIL:
					     mypDialog.dismiss(); 
					     Toast.makeText(ItemActivity.this, "对不起，关注失败", Toast.LENGTH_SHORT).show();	 
					     break;
					     
                   case RESULT_MESSAGE_OK:
                	     mypDialog.dismiss();
                	     Intent i=new Intent();
                	     i.setClass(ItemActivity.this, MessageOKActivity.class);
                	     ItemActivity.this.startActivityForResult(i, 1);
                	     ContextApplication.isMessageRefresh=true;
                	     Toast.makeText(ItemActivity.this, "恭喜您，留言成功", Toast.LENGTH_SHORT).show();
					     break; 
                   case RESULT_MESSAGE_FAIL:
					     mypDialog.dismiss(); 
					     Toast.makeText(ItemActivity.this, "对不起，留言失败", Toast.LENGTH_SHORT).show();	 
					     break;
					     
                     
				}
			}
		};
		
		
	private class Task extends AsyncTask<String, String, String> {
 
		@Override
		protected String doInBackground(String... params) {
			if(params[0].equals(TASK_ATTENTION)){
				return doAttentionPro();
			}else if(params[0].equals(TASK_MESSAGE)){
				return doGuestBook();
			}else if(params[0].equals(TASK_IMAGE)){
				return getShopImage();
			}else if(params[0].equals(TASK_LOAD_PIC)){
				return doGetProPicList();
			}else
				return ""; 
		} 
		@Override
		protected void onPostExecute(String result) {				
			super.onPostExecute(result);

			if(result.equals("fail")){
				Message msg = Message.obtain();
				msg.what = RESULT_LOAD_ITEMDETAIL_FAIL;
				handler.sendMessage(msg);
			}else if(result.equals("ok")){
				Message msg = Message.obtain();
				msg.what = RESULT_LOAD_ITEMDETAIL_OK;
				handler.sendMessage(msg);
			}else if(result.equals("ap_fail")){
				Message msg = Message.obtain();
				msg.what = RESULT_ATTENTION_FAIL;
				handler.sendMessage(msg);
			}else if(result.equals("ap_ok")){
				Message msg = Message.obtain();
				msg.what = RESULT_ATTENTION_OK;
				handler.sendMessage(msg);
			}
			else if(result.equals("gb_fail")){
				Message msg = Message.obtain();
				msg.what = RESULT_MESSAGE_FAIL;
				handler.sendMessage(msg);
			}else if(result.equals("gb_ok")){
				Message msg = Message.obtain();
				msg.what = RESULT_MESSAGE_OK;
				handler.sendMessage(msg);
			}else if(result.equals("image_ok")){
				try{
					if(logo!=null)
						im_logo.setBackgroundDrawable(logo);
				}catch(Exception e){}
			}else if(result.equals("pp_ok")){
				Message msg = Message.obtain();
				msg.what = RESULT_LOAD_ITEMDETAIL_OK;
				handler.sendMessage(msg);
			}
		} 
		@Override
		protected void onPreExecute() { 
			super.onPreExecute(); 
		    Message msg = Message.obtain();
		    if(preTask==0)
		        msg.what = ALERT_LOADING;
		    else if(preTask==5)
		    	msg.what = 12;
		    else
		    	msg.what = ALERT_MESSAGE;
			   handler.sendMessage(msg); 
		} 
		@Override
		protected void onProgressUpdate(String... values) {
			super.onProgressUpdate(values);
		}
	}
	Drawable logo=null;
	 public String getShopImage(){
		 String result="image_fail";
		 logo=null;
		 try{
			   String fileName=Utility.getPictureFileName(ContextApplication.project.getPro_pic_show()); 
				
				URL m;
				InputStream i = null; 
				try {
					if(!fileName.equals("")){
					   File f = new File(Environment.getExternalStorageDirectory(),"shengyijie/"+fileName);
					   FileInputStream is = null;
					   is = new FileInputStream(f);
					   Bitmap bm;
					   bm = BitmapFactory.decodeStream(is, null, null);
					   logo = new BitmapDrawable(bm);
					}
				}catch (Exception e) {}
				
				if(logo == null){
					try {
						m = new URL(ContextApplication.project.getPro_pic_show());
						i = (InputStream) m.getContent();
						logo = Drawable.createFromStream(i, "src");
						i.close();
						m=null;
						i=null;
						if(logo!=null){
							result="image_ok";
							if( !fileName.equals("")){ 
							   Utility.saveImage(fileName,logo);
							}
						}
						
					}catch (Exception e) {}
				} else{
					result="image_ok";
				}
			 
		}catch(Exception e){}
		return result;
	 }
   
    
}